//
//  ViewController.m
//  WDGroMoreDemo
//
//  Created by little People on 2023/8/2.
//

//#import "WDGroMore/WDGroMoreDelegateManager.h"

#define BMSName     [[NSBundle mainBundle].infoDictionary objectForKey:@"BMSName"]
#define BMSVersion  [[NSBundle mainBundle].infoDictionary objectForKey:@"BMSVersion"]


#import "ViewController.h"
#import <Masonry/Masonry.h>
#import <WDGroMore/WDGroMorePublic.h>
#import <WDGroMore/WDGroMoreDelegateManager.h>
#import <BUAdTestMeasurement/BUAdTestMeasurement.h>

@interface ViewController ()<WDGroMoreAllDelegate>

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self createUI];
}

- (void)createUI {
    NSArray *titlesAndSelectors = @[
        @{@"title": @"开屏广告", @"action": @"showSplashAd"},
        @{@"title": @"激励视频", @"action": @"showRewardedAd"},
        @{@"title": @"插全屏广告", @"action": @"showInterstitialAd"},
        @{@"title": @"Banner 广告", @"action": @"showBannerAd"},
        @{@"title": @"移除 Banner 广告", @"action": @"removeBannerAd"},
        @{@"title": @"信息流广告", @"action": @"showNativeAd"},
        @{@"title": @"信息流广告位移", @"action": @"ShowNativeAdXY"},
        @{@"title": @"移除信息流广告", @"action": @"removeNativeAd"},
        @{@"title": @"AD DEBUG", @"action": @"showDebugView"},
        @{@"title": @"SDK 初始化", @"action": @"SDKInit"},
        @{@"title": @"获取 UUID", @"action": @"WDGetUUID"},
        @{@"title": @"打开隐私页面", @"action": @"openPrivacy"},
        @{@"title": @"打开实名页面", @"action": @"openRealNamel"},
        @{@"title": @"打开服务条款WEB页面", @"action": @"openTermlURL"},
        @{@"title": @"打开隐私协议WEB页面", @"action": @"openPrivacyURl"},
        @{@"title": @"打开H5WEB页面", @"action": @"openH5WEB"},
        @{@"title": @"单次震动", @"action": @"vibrateOnce"},
        @{@"title": @"自定义震动", @"action": @"vibrateWithPattern"},
        @{@"title": @"取消震动", @"action": @"vibrateCancel"},
        @{@"title": @"数数埋点上报", @"action": @"TAEvent"}
    ];

    UIView *lastLeft = nil;
    UIView *lastRight = nil;

    CGFloat buttonWidth = (UIScreen.mainScreen.bounds.size.width - 40 - 20) / 2.0;

    for (int i = 0; i < titlesAndSelectors.count; i++) {
        NSDictionary *entry = titlesAndSelectors[i];
        UIButton *button = [self createButtonWithTitle:entry[@"title"] action:NSSelectorFromString(entry[@"action"])];
        [self.view addSubview:button];

        BOOL isLeft = (i % 2 == 0);

        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(buttonWidth);
            make.height.mas_equalTo(44);

            if (isLeft) {
                make.left.equalTo(self.view).offset(20);
                if (lastLeft) {
                    make.top.equalTo(lastLeft.mas_bottom).offset(20);
                } else {
                    make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(40);
                }
            } else {
                make.right.equalTo(self.view).offset(-20);
                if (lastRight) {
                    make.top.equalTo(lastRight.mas_bottom).offset(20);
                } else {
                    make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(40);
                }
            }
        }];

        if (isLeft) {
            lastLeft = button;
        } else {
            lastRight = button;
        }
    }
}

#pragma mark - 按钮创建方法

- (UIButton *)createButtonWithTitle:(NSString *)title action:(SEL)selector {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [button setBackgroundColor:[UIColor colorWithRed:0.2 green:0.6 blue:1 alpha:0.1]];
    button.layer.cornerRadius = 8;
    [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    return button;
}

#pragma mark - 按钮事件（你可替换为真实广告展示逻辑）

- (void)ShowNativeAdXY {
    [[WDGroMorePublic manager]showNativeAdAtPosition:10 yOffset:100];
}

- (void)removeNativeAd {
    [[WDGroMorePublic manager]removeNativeAd];
}

- (void)TAEvent {
    [[WDGroMorePublic manager]thinkingEventWithKey:@"TEST_TA" jsonStr:@{@"key":@"velue"}];
}

- (void)vibrateOnce {
    [[WDGroMorePublic manager]vibrateOnce];
}

- (void)vibrateWithPattern {
    [[WDGroMorePublic manager]vibrateWithPattern:@[@1000, @3000] repeat:10];
}

- (void)vibrateCancel {
    [[WDGroMorePublic manager]vibrateCancel];
}

- (void)openTermlURL {
    [[WDGroMorePublic manager]openTerms];
}

- (void)openPrivacyURl {
    [[WDGroMorePublic manager]openPrivacy];
}

- (void)openRealNamel {
    [[WDGroMorePublic manager]showRealnamePopupWithCompletion:^(BOOL success) {
        NSLog(@"实名=%id",success);
    }];
}

- (void)openPrivacy {
    [[WDGroMorePublic manager]showPrivacyPopupWithCompletion:^(BOOL agreed) {
        NSLog(@"隐私=%id",agreed);
    }];
}

- (void)openH5WEB {
    [[WDGroMorePublic manager]openH5WithUrl:@"http://web-page.zhuoxunhudong.com/web-page/3/privacy"];
}

- (void)showDebugView {
    [BUAdTestMeasurementConfiguration configuration].debugMode = YES;
    [BUAdTestMeasurementManager showTestMeasurementWithController:self];
}

- (void)WDGetUUID {
    NSLog(@"获取 UUID = %@",[[WDGroMorePublic manager]WDGetUUID]);
}

- (void)SDKInit {
    NSLog(@"SDK 初始化");
    [[WDGroMorePublic manager]initSDKCompletion:^(BOOL success, BOOL privacyAgreed, BOOL trackingAuthorized, BOOL didRealname, NSString * _Nonnull uuid) {
        if (privacyAgreed) {
            [WDGroMorePublic manager].uuid = uuid;
            
            [WDGroMorePublic manager].success = YES;
            
            [[WDGroMorePublic manager] GroMoreDelegate:self];
            
            [[WDGroMorePublic manager]initWindMillAdsWithNetworkDetection:YES];
            
            [[WDGroMorePublic manager]initAnalyticsSDK];
        }
    }];
}

- (void)showSplashAd {
    NSLog(@"展示开屏广告");
    [[WDGroMorePublic manager] tryShowSplashAdWithLaunch];

    // 调用 GroMore 开屏广告逻辑
}

- (void)showRewardedAd {
    NSLog(@"展示激励视频广告");
    [[WDGroMorePublic manager] showGroMoreRewardVideoAd];
    // 调用 GroMore 激励视频逻辑
}

- (void)showInterstitialAd {
    NSLog(@"展示插全屏广告");
    [[WDGroMorePublic manager] showGroMoreIntersititialAd];
    // 调用 GroMore 插屏广告逻辑
}

- (void)showBannerAd {
    NSLog(@"展示 Banner 广告");
    [[WDGroMorePublic manager] showGroMoreBannerAdWithTop:NO];
    // 调用 GroMore Banner 广告逻辑
}

- (void)removeBannerAd {
    NSLog(@"移除 banner 广告");
    [[WDGroMorePublic manager]GroMoreCloseBannerAd];
}

- (void)showNativeAd {
    NSLog(@"展示信息流广告");
    [[WDGroMorePublic manager]GroMoreShowNativeAd];
    // 调用 GroMore 信息流广告逻辑
}


- (void)encodeWithCoder:(nonnull NSCoder *)coder {
    
}

- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection {
    
}

- (void)preferredContentSizeDidChangeForChildContentContainer:(nonnull id<UIContentContainer>)container {
    
}

- (CGSize)sizeForChildContentContainer:(nonnull id<UIContentContainer>)container withParentContainerSize:(CGSize)parentSize {
    return CGSizeMake(0, 0);
}

- (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(nonnull id<UIContentContainer>)container {
    
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator {
    
}

- (void)willTransitionToTraitCollection:(nonnull UITraitCollection *)newCollection withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator {
    
}

- (void)didUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context withAnimationCoordinator:(nonnull UIFocusAnimationCoordinator *)coordinator {
    
}

- (void)setNeedsFocusUpdate {
    
}

- (BOOL)shouldUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context {
    return YES;
}

- (void)updateFocusIfNeeded {
    
}

@end

